//
//  ParticipationAwardViewController.m
//  homeland
//
//  Created by caesar on 14-5-9.
//  Copyright (c) 2014年 xzysun. All rights reserved.
//

#import "ParticipationAwardViewController.h"
#import "ParticipateService.h"
#import "AwardViewController.h"
#import "ParticipationSubViewController.h"
#import "AwardViewController.h"

#import "SGFocusImageItem.h"
#import "SGFocusImageFrame.h"

@interface ParticipationAwardViewController () <ParticipateServiceDelegate,SGFocusImageFrameDelegate>

@end

@implementation ParticipationAwardViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"参与有奖";
    // Do any additional setup after loading the view from its nib.
    int height = iPhone5 ? 0 : 88 ;


    UIButton *joinInBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 460 - height, 320, 44)];
    [joinInBtn setBackgroundImage:[UIImage imageNamed:@"btnImage.png"] forState:0];
    if (self.hasJoined) {
        [joinInBtn setTitle:@"我的推荐" forState:0];
    } else {
        [joinInBtn setTitle:@"参与" forState:0];
    }
    [joinInBtn addTarget:self action:@selector(joinInParticipation) forControlEvents:64];
    [self.view addSubview:joinInBtn];
    [self.backScrollView setScrollEnabled:NO];
    
    self.navigationItem.leftBarButtonItem = autoavBackButton(self, @selector(backMethod));
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"_navBar"] forBarMetrics:UIBarMetricsDefault];
    
    int length = 3;
    
    NSDictionary *object1 = [NSDictionary dictionaryWithObjects:@[ @"hswx_image", @"icon" ] forKeys:@[ @"value", @"title" ]];
    NSDictionary *object2 = [NSDictionary dictionaryWithObjects:@[ @"小米盒子宣传.jpg", @"scroll" ] forKeys:@[ @"value", @"title" ]];

    NSDictionary *object3 = [NSDictionary dictionaryWithObjects:@[ @"activity", @"html" ] forKeys:@[ @"value", @"title" ]];
    NSDictionary *object4 = [NSDictionary dictionaryWithObjects:@[ @"形象页4", @"形象页4" ] forKeys:@[ @"icon", @"title" ]];
    
    NSMutableArray *tempArray = [[NSMutableArray alloc] initWithArray:@[object1, object2, object3,object4]];
    
    NSMutableArray *itemArray = [NSMutableArray arrayWithCapacity:length + 3];
    if (length > 1) {
        NSDictionary *dict = [tempArray objectAtIndex:length - 1];
        SGFocusImageItem *item = [[SGFocusImageItem alloc] initWithDict:dict tag:-1];
        [itemArray addObject:item];
    }
    for (int i = 0; i < length; i++) {
        NSDictionary *dict = [tempArray objectAtIndex:i];
        SGFocusImageItem *item = [[SGFocusImageItem alloc] initWithDict:dict tag:i];
        [itemArray addObject:item];
    }
    
    if (length >1) {
        NSDictionary *dict = [tempArray objectAtIndex:0];
        SGFocusImageItem *item = [[SGFocusImageItem alloc] initWithDict:dict tag:length];
        [itemArray addObject:item];
    }
    SGFocusImageFrame *bannerView = [[SGFocusImageFrame alloc] initWithFrame:CGRectMake(0, 0, self.backScrollView.frame.size.width, 469 - height) delegate:self imageItems:itemArray isAuto:NO];
    bannerView.itemWidth = 320;
    [bannerView scrollToIndex:0];
    [bannerView._pageControl setCenter:CGPointMake(160, 469 - height - 28)];
    
    [self.backScrollView addSubview:bannerView];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];

    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"_navBar"] forBarMetrics:UIBarMetricsDefault];
    CGRect scrollViewFrame = self.backScrollView.frame;
    scrollViewFrame.origin.y = 0;
    self.backScrollView.frame = scrollViewFrame;
}

- (void)joinInParticipation
{
    if (self.hasJoined) {
        AwardViewController *viewController = [[AwardViewController alloc] initWithNibName:@"AwardViewController" bundle:nil];
        [self.navigationController pushViewController:viewController animated:YES];
    } else {
        [ParticipateService share].delegate = self;
        [[ParticipateService share] subParticipate];
    }
}

- (void)subSuccess:(BOOL)isSuccess
{
    if (!isSuccess) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"已参加活动" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
    } else {
        AwardViewController *viewController = [[AwardViewController alloc] initWithNibName:@"AwardViewController" bundle:nil];
        [self.navigationController pushViewController:viewController animated:YES];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
    self.navigationItem.rightBarButtonItem = getCustomNavButton(@"新增", self, @selector(rightButtonAction:));

    // Dispose of any resources that can be recreated.
}
#pragma mark - Right Button
-(void)rightButtonAction:(id)sender
{
    ParticipationSubViewController *vc = [[ParticipationSubViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
